﻿<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "Approval content"
		PageController.AuthorizedRoles =  Array("editor","publisher")
		pageController.RequiresAuthentication  = True
		PageController.PageCSSFiles = "main.css" 		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>Approval content</H2>
	

	
	<%Page.Execute%>	
	<Span Class="Caption">Approval<HR></Span>
	

	<%panel%>
	
	
<%End Function%>

<%  '
	
	Dim txtComment
	Dim title
	Dim requestComment
	Dim userRequest
	Dim Panel
	Dim contentID
	Dim mValidation

	Dim cmdAllow	
	Dim cmdDeny
	Dim recordID
	
		
	Public Function Page_Init()
		recordID = 0  'Init
		
		
		Set txtComment	= New_ServerTextBoxEX("txtComment",100,100)	
		
		Set cmdAllow = New_ServerLinkButton("cmdAllow")		
		Set cmdDeny = New_ServerLinkButton("cmdDeny")	
		
		Set mValidation = New ServerValidationSummary
		Set panel = New_ServerPanel("Panel",500,400)
		
	End Function

	Public Function Page_Controls_Init()		
		cmdAllow.Text = "Allow"	
		cmdDeny.Text = "Deny"			
	
		txtComment.Mode=3
		txtComment.Rows = 5
		txtComment.Cols = 40
		panel.Mode = 2
	
		panel.PanelTemplate = "RenderPanel"
	End Function

	Public Function Page_Load()	
		If Not Page.IsPostBack Then

			If Request.QueryString("id")<>"" Then
				recordID = Clng(Request.QueryString("id"))			
				 
			End if
			Call LoadRecord()
						
		End If	
	End Function
	
	Public Function Page_LoadViewState()
		recordID = Page.ViewState.GetValue("ID") 'Get from viewstate	
		

	End Function
	Public Function Page_PreRender()
		Page.ViewState.Add "ID",recordID 'Add to viewstate :-)
	
	End Function

	Public Function cmdAllow_OnClick()
		Dim sSQL
		Dim rs	
		
		Call Validate()
		If Not mValidation.IsValid Then
			Exit Function
		End If		
		sSQL = "SELECT * FROM approval Where id=" & recordID
		'Response.Write(sSQL)
		
		Set rs = DBLayer.GetRecordSet(sSQL)
		
		
		If rs.RecordCount = 0 Then
			rs.AddNew
		End If
		
		
		rs("approvalcomment").Value    = txtComment.Text	
		rs("userapproval").Value		= CurrentUser.UserID()
		rs("approvaltime").Value		= Now()		
		rs("status").Value = 20 '20 => allow publish
		contentID = rs("contentid").Value
		DBLayer.UpdateRecordSet rs
		
		sSQL = "SELECT * FROM content Where id=" & contentID
		Set rs = DBLayer.GetRecordSet(sSQL)
		If rs.RecordCount <> 0 Then
			rs("status").Value=3 ' allowed publish 
			DBLayer.UpdateRecordSet rs
		End If
		
		
		If mValidation.IsValid Then
			Response.Redirect "ListRequest.asp"
		End If
		
	End Function
	
	Public Function cmdDeny_OnClick()
		Dim sSQL
		Dim rs	
		
		Call Validate()
		If Not mValidation.IsValid Then
			Exit Function
		End If		
		sSQL = "SELECT * FROM approval Where id=" & recordID
		'Response.Write(sSQL)
		
		Set rs = DBLayer.GetRecordSet(sSQL)
		
		
		If rs.RecordCount = 0 Then
			rs.AddNew
		End If
		
		
		rs("approvalcomment").Value    = txtComment.Text	
		rs("userapproval").Value		= CurrentUser.UserID()
		rs("approvaltime").Value		= Now()		
		rs("status").Value = 30 '30 => deny publish
		contentID = rs("contentid").Value
		DBLayer.UpdateRecordSet rs
		
		sSQL = "SELECT * FROM content Where id=" & contentID
		Set rs = DBLayer.GetRecordSet(sSQL)
		If rs.RecordCount <> 0 Then
			rs("status").Value=4 ' Denied publish 
			DBLayer.UpdateRecordSet rs
		End If
		
		
		If mValidation.IsValid Then
			Response.Redirect "ListRequest.asp"
		End If
		
	End Function
	
	Public Function Validate()
		
		'You could use several approaches to validation. I normaly use this approach and modify the control output as appropriate
	
		'(object,strFriendlyName, intDataType, bolRequired, strDefaultValue)
		mValidation.Validate 	txtComment,"Request comment", VALIDATION_DATATYPE_STRING, True , ""
	
		
	End Function

	Public Function LoadRecord()
		Dim sSQL
		Dim rs
		
		
		sSQL = "SELECT c.title, a.*, u.username FROM approval as a inner join content as c on a.contentid = c.id " & _
		"inner join [user] as u on u.id =a.userrequest Where a.id=" & recordID
		Set rs = GetRecordSet(sSQL)
		if rs.RecordCount <>0 Then
			title = rs("title").Value
			requestComment = rs("requestcomment").Value
			userRequest = rs("username").Value		
		End if
			
	End Function

	
%>


<%Function RenderPanel() %>
<%mValidation%>
	<table border=0 ID="Table1">
		<tr><td>Content </td><td><a target="_blank" href="../Default.asp?act=content&id=<%Response.Write(recordID)%>"><%Response.Write(title)%></a></td></tr>
		<tr><td>Request Comment</td><td><%Response.Write(requestComment)%></td></tr>
		<tr><td>User request</td><td><%Response.Write(userRequest)%></td></tr>		
		<tr><td>Approval Comment</td><td><%txtComment%></td></tr>
		<tr><td colspan=2 align=left><a href="Content.asp"><HR>Back</a> | <%cmdAllow%>| <%cmdDeny%></td></tr>
	</table>
	
<%End Function%>
